'use strict';

const util = require('../util/util')
// const Controller = require('egg').Controller;
const Controller = require('./base');
/**
 * @controller Authorize
 */
class HomeController extends Controller {

  /**
  * @summary authorize
  * @description authorize
  * @router GET /judge
  * @Request query string contactId ID eg:2001   
  * @Request query string *Id ID eg:200
  * @consumes html/text applicatoin/json
  * @response 200 user  
  */ 

  async index() {
    const { ctx } = this; 
    try {
        // ctx.validate({ name: {required: true} } ) //ctx.query  ctx.params ctx.request.body
        ctx.validate({ name: {required: true,type:'string'} }, util.args(this.ctx)  ) //ctx.query  ctx.params ctx.request.body
        this.success('hi, egg')
    } catch(err) {
        console.log(err)
        ctx.body = { code: 0, error: err.errors }
    } 
  }
  
  async index1() {
    const { ctx } = this; 
    const { name } = util.args(this.ctx);  
    if(util.checkArgs(this.ctx,['name'])) return;
    // ctx.body = 'hi, egg'; 
    this.success('hi, egg')
  }
  
  async mysql() {
    const { ctx } = this;
    const conn=await this.app.mysql.beginTransaction();
    let response;
    try{ 
        // await conn.insert('user',{'username':'xiao1','password':'1111'});
        response = await conn.select("app",{  
          where:{id: 30 },
          columns:['id','version'],
          orders: [['create_time','desc'], ['id','desc']],
          limit:10,
          offset:0,
         })
        // await conn.update('user',{id:2,username:'黑子'});
        // await conn.delete(table,{ id });
        await conn.commit();  //提交事务
    }catch(err){
        await conn.rollback();//回滚事务
        throw err;
    }
    ctx.body = response; 
  }
}

module.exports = HomeController;
